home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap11 / PopPad3 / PopFind.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  3.4 KB  |  116 lines

  1. /*--------------------------------------------------------
  2.    POPFIND.C -- Popup Editor Search and Replace Functions
  3.   --------------------------------------------------------*/
  4.  
  5. #include <windows.h>
  6. #include <commdlg.h>
  7. #include <tchar.h>            // for _tcsstr (strstr for Unicode & non-Unicode)
  8.  
  9. #define MAX_STRING_LEN   256
  10.  
  11. static TCHAR szFindText [MAX_STRING_LEN] ;
  12. static TCHAR szReplText [MAX_STRING_LEN] ;
  13.  
  14. HWND PopFindFindDlg (HWND hwnd)
  15. {
  16.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  17.      
  18.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  19.      fr.hwndOwner        = hwnd ;
  20.      fr.hInstance        = NULL ;
  21.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  22.      fr.lpstrFindWhat    = szFindText ;
  23.      fr.lpstrReplaceWith = NULL ;
  24.      fr.wFindWhatLen     = MAX_STRING_LEN ;
  25.      fr.wReplaceWithLen  = 0 ;
  26.      fr.lCustData        = 0 ;
  27.      fr.lpfnHook         = NULL ;
  28.      fr.lpTemplateName   = NULL ;
  29.      
  30.      return FindText (&fr) ;
  31. }
  32.  
  33. HWND PopFindReplaceDlg (HWND hwnd)
  34. {
  35.      static FINDREPLACE fr ;       // must be static for modeless dialog!!!
  36.      
  37.      fr.lStructSize      = sizeof (FINDREPLACE) ;
  38.      fr.hwndOwner        = hwnd ;
  39.      fr.hInstance        = NULL ;
  40.      fr.Flags            = FR_HIDEUPDOWN | FR_HIDEMATCHCASE | FR_HIDEWHOLEWORD ;
  41.      fr.lpstrFindWhat    = szFindText ;
  42.      fr.lpstrReplaceWith = szReplText ;
  43.      fr.wFindWhatLen     = MAX_STRING_LEN ;
  44.      fr.wReplaceWithLen  = MAX_STRING_LEN ;
  45.      fr.lCustData        = 0 ;
  46.      fr.lpfnHook         = NULL ;
  47.      fr.lpTemplateName   = NULL ;
  48.      
  49.      return ReplaceText (&fr) ;
  50. }
  51.  
  52. BOOL PopFindFindText (HWND hwndEdit, int * piSearchOffset, LPFINDREPLACE pfr)
  53. {
  54.      int    iLength, iPos ;
  55.      PTSTR  pstrDoc, pstrPos ;
  56.      
  57.           // Read in the edit document
  58.      
  59.      iLength = GetWindowTextLength (hwndEdit) ;
  60.      
  61.      if (NULL == (pstrDoc = (PTSTR) malloc ((iLength + 1) * sizeof (TCHAR))))
  62.           return FALSE ;
  63.      
  64.      GetWindowText (hwndEdit, pstrDoc, iLength + 1) ;
  65.      
  66.           // Search the document for the find string
  67.      
  68.      pstrPos = _tcsstr (pstrDoc + * piSearchOffset, pfr->lpstrFindWhat) ;
  69.      free (pstrDoc) ;
  70.      
  71.           // Return an error code if the string cannot be found
  72.      
  73.      if (pstrPos == NULL)
  74.           return FALSE ;
  75.      
  76.           // Find the position in the document and the new start offset
  77.      
  78.      iPos = pstrPos - pstrDoc ;
  79.      * piSearchOffset = iPos + lstrlen (pfr->lpstrFindWhat) ;
  80.      
  81.           // Select the found text
  82.      
  83.      SendMessage (hwndEdit, EM_SETSEL, iPos, * piSearchOffset) ;
  84.      SendMessage (hwndEdit, EM_SCROLLCARET, 0, 0) ;
  85.      
  86.      return TRUE ;
  87. }
  88.  
  89. BOOL PopFindNextText (HWND hwndEdit, int * piSearchOffset)
  90. {
  91.      FINDREPLACE fr ;
  92.      
  93.      fr.lpstrFindWhat = szFindText ;
  94.      
  95.      return PopFindFindText (hwndEdit, piSearchOffset, &fr) ;
  96. }
  97.  
  98. BOOL PopFindReplaceText (HWND hwndEdit, int * piSearchOffset, LPFINDREPLACE pfr)
  99. {
  100.           // Find the text
  101.      
  102.      if (!PopFindFindText (hwndEdit, piSearchOffset, pfr))
  103.           return FALSE ;
  104.      
  105.           // Replace it
  106.      
  107.      SendMessage (hwndEdit, EM_REPLACESEL, 0, (LPARAM) pfr->lpstrReplaceWith) ;
  108.      
  109.      return TRUE ;
  110. }
  111.  
  112. BOOL PopFindValidFind (void)
  113. {
  114.      return * szFindText != '\0' ;
  115. }
  116.